/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewProduct.java
 *
 * Created on Oct 2, 2014, 4:02:28 PM
 */
package com.JD.master.frame;

import com.JD.datePicker.DatePicker;
import com.JD.master.Hibernate.ProductMaster;
import java.awt.print.PrinterException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author admin
 */
public class AddNewProduct extends javax.swing.JFrame {

    /** Creates new form AddNewProduct */
    public AddNewProduct() {
        initComponents();

        com.JD.datePicker.DatePicker datePicker = new DatePicker();
        List year_List = datePicker.getYear_List();
        List day_List = datePicker.getDay_List();


        for (Object object : year_List) {

            year_Combobox.addItem(object.toString());
        }

        for (Object object : day_List) {

            date_ComboBox.addItem(object.toString());

        }

        String month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

        month_Combobox.addItem(month[Integer.parseInt(new java.text.SimpleDateFormat("MM").format(new Date())) - 1]);
        for (String string : month) {

            month_Combobox.addItem(string);
        }


        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();

        //com.JD.master.Hibernate.ProductMaster
        Query query = session.createQuery("from com.JD.master.Hibernate.ProductMaster");

        List list = query.list();

        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) item_Table.getModel();




        for (Object object : list) {

            com.JD.master.Hibernate.ProductMaster productMaster = (com.JD.master.Hibernate.ProductMaster) object;
            model.insertRow(0, new Object[]{productMaster.getProductItem(), productMaster.getProductType()});

        }




    }
    com.JD.master.Validation.Validator validator = new com.JD.master.Validation.Validator();
    String product_Item = "NO";
    String product_Type = "NO";
    String rawField1 = "NO";
    String rawField2 = "NO";
    String rawField3 = "NO";
    String rawField4 = "NO";

    public JPanel getAddNewProduct_Panel() {
        return AddNewProduct_Panel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AddNewProduct_Panel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        item_Table = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        itemName_TextField = new javax.swing.JTextField();
        itemType_ComboBox = new javax.swing.JComboBox();
        addDataToDataBase_Button = new javax.swing.JButton();
        year_Combobox = new javax.swing.JComboBox();
        date_ComboBox = new javax.swing.JComboBox();
        month_Combobox = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        AddNewProduct_Panel.setBackground(new java.awt.Color(255, 102, 51));
        AddNewProduct_Panel.setPreferredSize(new java.awt.Dimension(1135, 627));

        jLabel1.setFont(new java.awt.Font("Traditional Arabic", 1, 24));
        jLabel1.setForeground(new java.awt.Color(18, 68, 200));
        jLabel1.setText("ADD NEW PRODUCT");

        item_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Item Name", "Item Type"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(item_Table);

        jLabel2.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel2.setForeground(new java.awt.Color(18, 68, 200));
        jLabel2.setText("Item Name:");

        jLabel3.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel3.setForeground(new java.awt.Color(18, 68, 200));
        jLabel3.setText("Item Type:");

        itemName_TextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                itemName_TextFieldKeyReleased(evt);
            }
        });

        itemType_ComboBox.setEditable(true);
        itemType_ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "RAW", "FINISH", "FUEL", "OTHER" }));

        addDataToDataBase_Button.setText("Add Data");
        addDataToDataBase_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDataToDataBase_ButtonActionPerformed(evt);
            }
        });

        year_Combobox.setEditable(true);

        date_ComboBox.setEditable(true);

        month_Combobox.setEditable(true);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/JD/PlaceOrder/excel.png"))); // NOI18N
        jButton1.setText("Export To Excel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/JD/PlaceOrder/print.png"))); // NOI18N
        jButton2.setText("Print");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AddNewProduct_PanelLayout = new javax.swing.GroupLayout(AddNewProduct_Panel);
        AddNewProduct_Panel.setLayout(AddNewProduct_PanelLayout);
        AddNewProduct_PanelLayout.setHorizontalGroup(
            AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AddNewProduct_PanelLayout.createSequentialGroup()
                .addGap(242, 242, 242)
                .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(AddNewProduct_PanelLayout.createSequentialGroup()
                        .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(58, 58, 58)
                        .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(itemName_TextField)
                            .addComponent(itemType_ComboBox, 0, 430, Short.MAX_VALUE)
                            .addGroup(AddNewProduct_PanelLayout.createSequentialGroup()
                                .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(year_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(addDataToDataBase_Button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(AddNewProduct_PanelLayout.createSequentialGroup()
                                        .addComponent(month_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(27, 27, 27)
                                        .addComponent(date_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jButton1))))
                        .addGap(10, 10, 10)
                        .addComponent(jButton2))
                    .addGroup(AddNewProduct_PanelLayout.createSequentialGroup()
                        .addGap(198, 198, 198)
                        .addComponent(jLabel1)))
                .addGap(411, 411, 411))
            .addGroup(AddNewProduct_PanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(222, Short.MAX_VALUE))
        );
        AddNewProduct_PanelLayout.setVerticalGroup(
            AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AddNewProduct_PanelLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addGap(35, 35, 35)
                .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(itemName_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemType_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(year_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(date_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(month_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(AddNewProduct_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addDataToDataBase_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(AddNewProduct_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, 1120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(AddNewProduct_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, 704, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemName_TextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_itemName_TextFieldKeyReleased
        // TODO add your handling code here:
        itemName_TextField.setText(validator.stringValidator(itemName_TextField.getText()).toUpperCase());

    }//GEN-LAST:event_itemName_TextFieldKeyReleased

    private void addDataToDataBase_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDataToDataBase_ButtonActionPerformed
        // TODO add your handling code here:

        product_Item = itemName_TextField.getText();
        product_Type = itemType_ComboBox.getSelectedItem().toString();

        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        com.JD.master.Hibernate.ProductMaster productMaster = new ProductMaster(product_Item, product_Type, rawField1, rawField2, rawField3, rawField4);
        session.save(productMaster);
        transaction.commit();

        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) item_Table.getModel();

        model.insertRow(0, new Object[]{product_Item, product_Type});

        itemName_TextField.setText("");

        itemType_ComboBox.removeAllItems();

        itemType_ComboBox.addItem("RAW");
        itemType_ComboBox.addItem("FINISH");
        itemType_ComboBox.addItem("FUEL");
        itemType_ComboBox.addItem("OTHER");



    }//GEN-LAST:event_addDataToDataBase_ButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        new com.JD.ExportToExcel.ExportToExcel().saveToExcel(item_Table);


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            // TODO add your handling code here:
            item_Table.print();
        } catch (PrinterException ex) {
           JOptionPane.showMessageDialog(null, "Please Cheack Your Printer..");

        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddNewProduct().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JPanel AddNewProduct_Panel;
    private javax.swing.JButton addDataToDataBase_Button;
    private javax.swing.JComboBox date_ComboBox;
    private javax.swing.JTextField itemName_TextField;
    private javax.swing.JComboBox itemType_ComboBox;
    private javax.swing.JTable item_Table;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox month_Combobox;
    private javax.swing.JComboBox year_Combobox;
    // End of variables declaration//GEN-END:variables
}
